<?
function createThumbs($pathHires, $path1024, $path350, $path90) {
	//------------- 1024 ---------------//
	scaleImage($pathHires, $path1024, 1024, 1024);
				
	//------------- 350 ---------------//
	scaleImage($path1024, $path350, 350, 350);
	expandWithWhite($path350, 350, 350);
	addFrame($path350, 150, 150, 150);
	addReflection($path350);
	
	//------------- 90 ---------------//
	cropImage($path1024, $path90, 90, 90);
	expandWithWhite($path90, 90, 90);
	addReflection($path90);
}

// Skalar om en bild till angivet format, dock ej förstora, endast jpeg
function scaleImage($pathSrc, $pathDst, $maxW, $maxH) {

	list($srcW, $srcH) = getimagesize($pathSrc);
	
	if($srcW > $maxW || $srcH > $maxH) {
		if(($srcW / $maxW) >= ($srcH / $maxH)) {				
			$dstW = $maxW;
			$dstH = ($dstW / $srcW) * $srcH;
		} else {				
			$dstH = $maxH;
			$dstW = ($dstH / $srcH) * $srcW;
		}
	} else {
		$dstW = $srcW;
		$dstH = $srcH;
	}
	
	$src = imagecreatefromjpeg($pathSrc);
	$dst = imagecreatetruecolor($dstW, $dstH);
	
	imagecopyresampled($dst, $src, 0, 0, 0, 0, $dstW, $dstH, $srcW, $srcH);
	
	imagejpeg($dst, $pathDst, 100);
	
	imagedestroy($src);
	imagedestroy($dst);
}

// Beskär en bild till angivet format, fyller ut med vitt om så behövs, endast jpeg
function cropImage($pathSrc, $pathDst, $dstW, $dstH) {
	list($srcW, $srcH) = getimagesize($pathSrc);
	
	if(($dstW / $srcW) >= ($dstH / $srcH)) { // Stående
		$srcX = 0;
		$srcY = ($srcH - $srcW) / 2;
		$srcH = $srcW;
	} else { // Liggande
		$srcX = ($srcW - $srcH) / 2;
		$srcY = 0;
		$srcW = $srcH;
	}
		
	$src = imagecreatefromjpeg($pathSrc);
	$dst = imagecreatetruecolor($dstW, $dstH);
	
	// Fyll överflödet med vitt
	$bgColor = imagecolorallocate($dst, 255, 255, 255);
	imagefilledrectangle($dst, 0, 0, $dstW, $dstH, $bgColor);
	
	imagecopyresampled($dst, $src, 0, 0, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
	
	
	// Spara bilden
	imagejpeg($dst, $pathDst, 100);
	
	imagedestroy($dst);
	imagedestroy($src);
}

// Fyller t ex 350x200 => 350x350 med vitt
function expandWithWhite($imgPath, $width, $height) {
	list($srcW, $srcH) = getimagesize($imgPath);
	
	$src = imagecreatefromjpeg($imgPath);
	$dst = imagecreatetruecolor($width, $height);
	
	$dstX = ($width - $srcW) / 2;
	$dstY = ($height - $srcH) / 2;
	
	$bgColor = imagecolorallocate($dst, 255, 255, 255);
	imagefilledrectangle($dst, 0, 0, 350, 350, $bgColor);
		
	imagecopyresampled($dst, $src, $dstX, $dstY, 0, 0, $srcW, $srcH, $srcW, $srcH);
	
	imagejpeg($dst, $imgPath, 100);
	
	imagedestroy($dst);
	imagedestroy($src);
}

// Lägger en ram runt en bild
function addFrame($imgPath, $red, $green, $blue) {
	list($width, $height) = getimagesize($imgPath);
	
	$img = imagecreatefromjpeg($imgPath);
	
	$borderColor = imagecolorallocate($img, $red, $green, $blue);
	
	imageline($img, 0, 0, 0, $height - 1, $borderColor);
	imageline($img, 0, $height - 1, $width - 1, $height - 1, $borderColor);
	imageline($img, $width - 1, $height - 1, $width - 1, 0, $borderColor);
	imageline($img, $width - 1, 0, 0, 0, $borderColor);
	
	imagejpeg($img, $imgPath, 100);
	
	imagedestroy($img);
}

// Reflektion under bilden a la Apple, 30 pixlar hög, oavsett bildstorlek
function addReflection($imgPath) {
	list($width, $height) = getimagesize($imgPath);
	
	$src = imagecreatefromjpeg($imgPath);
	$dst = imagecreatetruecolor($width, $height + 30);
	
	$bgColor = imagecolorallocate($dst, 255, 255, 255);
	imagefilledrectangle($dst, 0, 0, $width - 1, $height + 29, $bgColor);
	
	imagecopy($dst, $src, 0, 0, 0, 0, $width, $height);
	
	$i = 1;
	
	for($y = ($height - 31); $y < ($height - 1); $y++) {
		$opacity = sin(M_PI * (($i / 30) * 0.2));
			
		imagecopymerge($dst, $src, 0, ($height * 2) - $y - 2, 0, $y, $width, 1, $opacity * 100);
		
		$i++;
	}
	
	//header('Content-Type: image/jpeg');
	imagejpeg($dst, $imgPath, 100);
	
	imagedestroy($src);
	imagedestroy($dst);
}
?>